/**
 * 合同签订
 */
import React, {Component} from 'react';

import './index.less';

import {
    base
} from 'nc-lightapp-front';
import AddForm from "../../../change/main/components/AddNew/formView";
import ExterrndAddForm from "../../../extend/main/components/AddNew/formView";
import MainCard from "../../../terminate/main/components/mainCard";
import {SIGNPARAM, releaseParam, finishParam, changeParam, extendParam} from "./common";
// import FormCard from "../../../sign/main/components/FormCard";
import SignModal from "../../../sign/main/components/SignModal";
import {jump} from "../../main/common";

const {
    NCModal,
    NCButton
} = base;

const {
    Header: NCModalHeader,
    Body: NCModalBody,
    Footer: NCModalFooter
} = NCModal;
let changeForm = ['dataBase', 'dataBefore', 'dataAfter'];
let finishForm = 'card';
// 业务处理 对应的title
const transactionTitle = {
    sign: 'cm6011-000059',
    change: 'cm6011-000091',
    extend: 'cm6011-000092',
    release: 'cm6011-000093',
    finish: 'cm6011-000094',
    contopinion: 'cm6011-000095',
};

class TransactionModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            json: props.json || {}
        }
    }

    componentWillReceiveProps(nextprops) {
        let nextjson = JSON.stringify(nextprops.json);
        let thisjson = JSON.stringify(this.props.json);
        if (nextjson !== thisjson) {
            this.setState({
                json: nextprops.json,
            })
        }

    }

    /**
     * 初始化函数
     */
    initPage = (extendtype) => {
        let props = this.props;
        let checkedRows = props.editTable.getCheckedRows('psninfo');
        // 签订
        if (props.transactionModelId === 'sign') {
            this.signNode.getFormCardData(checkedRows)
        }
        // 变更
        if (props.transactionModelId === 'change') {
            this.changeNode.nextHandle(checkedRows)
        }
        // 续签
        if (props.transactionModelId === 'extend') {
            this.extendNode.nextHandle(checkedRows, extendtype)
        }
        // 解除
        if (props.transactionModelId === 'release') {
            this.releaseNode.nextHandle(checkedRows)
        }
        // 终止
        if (props.transactionModelId === 'finish') {
            this.finishNode.nextHandle(checkedRows)
        }
    };

    // 确定
    confirm = () => {
        if (this.props.transactionModelId === 'sign') {
            this.signNode.doneHandle();
        }
        if (this.props.transactionModelId === 'change') {
            this.changeNode.doneHandle();
        }
        if (this.props.transactionModelId === 'extend') {
            this.extendNode.doneHandle(this.props.extendtype);
        }
        if (this.props.transactionModelId === 'release') {
            this.releaseNode.done();
        }
        if (this.props.transactionModelId === 'finish') {
            this.finishNode.done();
        }
    };

    /**
     * 关闭本谈弹窗
     */
    closeInfoModal = () => {
        // this.props.form.EmptyAllFormValue(this.props.formId);
        this.props.updateState({
            transactionVisible: false,
            transactionModelId: null
        });
        if (this.props.transactionModelId === 'sign'){
            this.signNode.cancelOccupy();
        }
    };

    /**
     * 合同签订 关闭窗口
     * @param jobpks =>返回的数据
     * @param agFlag
     */
    closeModal = () => {
        this.closeAddRefresh();
    };

    /**
     * 关闭后刷新列表
     */
    closeAddRefresh = () => {
        this.closeInfoModal();
        this.props.refresh();
    };

    /**
     * 跳转到 某一节点
     * @param pk_psndoc_subs  {Array} 合同主键
     * @param nodeName {String} 要跳转到的节点名称，例如跳到签订页面：'sign'
     * @param isOther {Boolean} 是否是其他页面 默认false： nodeName形式不变'sign'；   true： nodeName 值变为 '/nccloud/resources/hrcm/contractmgt/sign/main/index.html'
     */
    jumpToNode = (pk_psndoc_subs, nodeName, isOther = false) => {
        let pageUrl = `/nccloud/resources/hrcm/contractmgt/${nodeName}/main/index.html`;
        if (isOther) {
            pageUrl = nodeName;
        }
        jump(pk_psndoc_subs, this.props.hrorgobj, pageUrl);
        this.closeInfoModal();
    };

    render() {
        const {
            transactionVisible
        } = this.props;

        let titleKey = transactionTitle[this.props.transactionModelId];

        // 变更续签组件参数
        let changeExtendParam = {
            ...this.props,
            pkOrg: this.props.hrorgval,
            pk_psndoc_sub: this.props.pk_psndoc_sub,
            setNewIncreasedDis: this.closeInfoModal,
            jumpToNode: this.jumpToNode,
            refreshTableList: this.props.refresh
        };
        return (
            <NCModal
                backdrop="static"
                show={transactionVisible}
                size="lg"
                className="transaction-module"
                onHide={this.closeInfoModal}
                // onEntered={this.showHandle}
            >
                <NCModalHeader
                    closeButton={true}
                >
                    {this.state.json[titleKey]}{/* 国际化处理： 合同签订*/}
                </NCModalHeader>
                <NCModalBody>
                    <div className="transaction-module-body">
                        {/* 签订 */}
                        {this.props.transactionModelId === 'sign' && <SignModal
                            {...this.props}
                            pkOrg={this.props.hrorgval}
                            {...SIGNPARAM}
                            closeFormCardModal={this.closeModal}
                            jumpToNode={this.jumpToNode}
                            ref={node => {
                                this.signNode = node
                            }}
                        />}
                        {/* 变更 */}
                        {this.props.transactionModelId === 'change' && <AddForm
                            {...changeExtendParam}
                            config={changeParam}
                            ref={node => {
                                this.changeNode = node
                            }}
                        />}
                        {/* 续签 */}
                        {this.props.transactionModelId === 'extend' && <ExterrndAddForm
                            {...changeExtendParam}
                            config={extendParam}
                            ref={node => {
                                this.extendNode = node
                            }}
                        />}

                        {/* 解除 */}
                        {this.props.transactionModelId === 'release' && <MainCard
                            {...this.props}
                            pkOrg={this.props.hrorgval}
                            closeModel={this.closeAddRefresh}
                            jumpToNode={this.jumpToNode}
                            {...releaseParam}
                            ref={node => {
                                this.releaseNode = node
                            }}
                        />}
                        {/*  终止 */}
                        {this.props.transactionModelId === 'finish' && <MainCard
                            {...this.props}
                            pkOrg={this.props.hrorgval}
                            closeModel={this.closeAddRefresh}
                            jumpToNode={this.jumpToNode}
                            {...finishParam}
                            ref={node => {
                                this.finishNode = node
                            }}
                        />}
                    </div>
                </NCModalBody>
                <NCModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.confirm}
                    >
                        {this.state.json['cm6011-000028']}{/* 国际化处理： 确认*/}
                    </NCButton>
                    <NCButton
                        colors="secondary"
                        onClick={this.closeInfoModal}
                    >
                        {this.state.json['cm6011-000054']}{/* 国际化处理： 取消*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default TransactionModal
